package com.farewell.hotel.controller;

import com.farewell.hotel.entity.InfoSearch;
import com.farewell.hotel.pojo.Vip;
import com.farewell.hotel.service.VipService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Farewell
 * @date 2021/4/25
 */
@Controller
public class VipController {
    @Autowired
    private VipService vipService;

    @RequestMapping("getVip.do")
    @ResponseBody
    public PageInfo<Vip> getVip(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                                @RequestBody InfoSearch vipInfoSearch) {
        if (vipInfoSearch.getConditionType() == null || vipInfoSearch.getKeyword() == null) {
            vipInfoSearch = null;
        }
        if (vipInfoSearch != null && (vipInfoSearch.getConditionType().isEmpty() || vipInfoSearch.getKeyword().isEmpty())) {
            vipInfoSearch = null;
        }
        PageHelper.startPage(pageNum, 10);
        List<Vip> vipList = vipService.findVipsByCondition(vipInfoSearch);
        return new PageInfo<>(vipList);
    }

    @RequestMapping("deleteVip.do")
    @ResponseBody
    public Boolean deleteVip(@RequestParam("id") Long id) {
        try {
            vipService.deleteVipById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping("addVip.do")
    @ResponseBody
    public Boolean addVip(@RequestBody Vip vip) {
        try {
            vipService.addVip(vip);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return true;
    }

    @RequestMapping("updateVip.do")
    @ResponseBody
    public Boolean updateVip(@RequestBody Vip vip) {
        try {
            vipService.updateVip(vip);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping("loadVip.do")
    @ResponseBody
    public List<Vip> loadVip() {
        return vipService.findVipsByCondition(null);
    }

    @RequestMapping("findVipById.do")
    @ResponseBody
    public Vip findVipById(@RequestParam("id") Long id) {
        return vipService.findVipById(id);
    }
}
